package com.yixin.vuehr.web.controller.acc;

import com.yixin.vuehr.bean.RespBean;
import com.yixin.vuehr.model.Account;
import com.yixin.vuehr.service.AccountService;
import com.yixin.vuehr.util.OpreateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/acc/basic")
public class AccountBasicController {
    @Autowired
    AccountService accountService;
    @GetMapping("/")
    public List<Account> getAllAccount(){
        return accountService.getAllAccount();
    }
    @PostMapping("/")
    public RespBean addAccount(@RequestBody Account account){
        if (accountService.addAccount(account)==1){
            OpreateUtil.addOp("记了一笔帐");
            return RespBean.ok("添加成功");
        }
        return RespBean.error("添加失败");
    }

    @PutMapping("/")
    public RespBean updateAccount(@RequestBody Account account){
        if (accountService.updateAccount(account)==1){
            OpreateUtil.addOp("跟新一笔帐");
            return RespBean.ok("跟新成功");
        }
        return RespBean.error("跟新失败");
    }

    @DeleteMapping("/{id}")
    public RespBean deleteAccount(@PathVariable Integer id){
        if (accountService.deleteAccount(id)==1){
            OpreateUtil.addOp("删了一笔帐");
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }

}
